package cn.tedu.file;

import java.io.*;
import java.util.Scanner;

/**
 * @author :Colin
 * @version 创建时间: 2021/6/17 17:12
 */
/*本类用于拓展练习，文件复制案例*/
public class TestCopyFile {
    public static void main(String[] args) {
        System.out.println("请输入源文件路径：");
        String f =new Scanner(System.in).nextLine();
        System.out.println("请输入目标文件路径");
        String t =new Scanner(System.in).nextLine();

        ZFCopy(f,t);
    }

    private static void ZFCopy(String f, String t) {
        Reader in = null;
        Writer out = null;
        try {
            in = new BufferedReader(new FileReader(f));
            out = new BufferedWriter(new FileWriter(t));
            int b ;
            while((b=in.read())!=-1){
                out.write(b);
            }
            System.out.println("文件复制成功");
        } catch (Exception e) {
            System.out.println("文件复制失败");
            e.printStackTrace();
        }finally{
            /*关流顺序，如果有多个流的话，最后创建的流先关，多条关流语句需要各自try-catch*/
            try {
                out.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
